// pages/adduse/adduse.js
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
	  array: [ '国有企业', '合作', '私营','合资'],
    array2:[],
	  index:0,
    index2:0,
    clientName :null,
    regdate : null,
    registeredFund: null,
    person: null,
    name: null,
    phone: null,
    area: null,
    formLack:false,

  },
  bindPickerChange: function(e) {
	console.log(e);
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  bindPickerChange2: function (e) {
    console.log(e);
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index2: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.getDictByCode("gds");
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getDictByCode("gds");
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  formSubmit: function (e) {
    var that = this; 
   

    wx.showToast({
      icon: 'loading',
      title: 'Loading',
      duration: 2000,
      mask: true
    })

    console.log(this.data.clientName);
    console.log('form发生了submit事件，携带数据为：', e.detail.value);
    let { clientName, regdate, registeredFund, person, name, phone, area} = e.detail.value;
    if (!clientName ) {
      wx.showToast({
        title: '请填写客户名称！',
        icon: 'none',
        duration: 2000
      })
      //wx.hideToast();
      return;
    }
    wx.request({
      url: app.globalData.apiUrl + '/wx/addClientDo', //接口地址
      data: {
        loginName: app.globalData.loginName,
        clientName: clientName,
        regdate: regdate,
        registeredFund: registeredFund,
        person: person,
        name: name,
        phone: phone,
        area: area
      },
      success: function (res) {
        console.log("新增客户信息=", res)
       if(res.data.code == 0){
         that.setData({
           clientName: null,
           regdate: null,
           registeredFund: null,
           name: null,
           phone: null,
           index: 0,
           index2: 0,

         })
         wx.showToast({
           title: '操作成功！',
           icon: 'success',
           duration: 2000
         })

       }else{
         wx.showToast({
           title: res.data.msg,
           icon: 'none',
           duration: 2000
         })
       }
        
      }
    })
  },  

  getDictByCode: function(codes){
    var that = this; 
    wx.request({
      url: app.globalData.apiUrl + '/wx/getDictByCode', //接口地址
      data: {
        code: codes,
      },
      success: function (res) {
        console.log("数据字典=", res)
        that.setData({
          array2: res.data.data,
        })
      }
    })
  }
})